<?php
	require('config.php');

	$shopId=$_REQUEST['shopId'];
	$youhui_id=$_REQUEST['youhui_id'];
	$yxiaoji=$_REQUEST['yxiaoji'];
	$hxiaoji=$_REQUEST['hxiaoji'];
	$psway=$_REQUEST['psway'];
	$liuyan=$_REQUEST['liuyan'];
	$ps_time2=$_REQUEST['ps_time2'];
	$address_id=$_REQUEST['address_id'];
	$goods_id=$_REQUEST['goods_id'];
	$num=$_REQUEST['num'];
	$user_id=$_REQUEST['user_id'];
	$time=time();
	$goodss_attr_id="";
	$goods_attr="";
	if(isset($_REQUEST['goodss_attr_id'])) $goodss_attr_id=$_REQUEST['goodss_attr_id'];
	if(isset($_REQUEST['goods_attr'])) $goods_attr=$_REQUEST['goods_attr'];
	$sql="select a.*,b.goodsFlag,b.goodsName,b.goodsImg,b.goodsProportion from `".$oto."_goods_seckill` as a,`".$oto."_goods` as b where a.`goodsId`='".$goods_id."' and a.goodsId=b.goodsId LIMIT 1";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$goods_group[]=$row;//将取得的所有数据赋值给person_info数组
	}
	$sql="select o.orderId,g.goodsNums from `".$oto."_orders` as o,`".$oto."_order_goods` as g where o.orderId=g.orderId and o.orderType='2' and g.goodsId='{$goods_id}' and o.orderStatus in ('-2','0','1','2','3','4') and userId='{$user_id}' ";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$order_yimai[]=$row;//将取得的所有数据赋值给person_info数组
	}
	/**********获取店铺基本信息***********/
	$sql="SELECT * FROM ".$oto."_shops where shopId='{$shopId}' LIMIT 1";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$shop_info[]=$row;//将取得的所有数据赋值给person_info数组
	}
	if(isset($goods_group))
	{
		if($goods_group[0]['goodsFlag']==-1)
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="商品不存在";
			echo json_encode($data);
			die();
		}
		if($goods_group[0]['seckillEndTime']<$time)
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="活动已结束";
			echo json_encode($data);
			die();
		}
		if($goods_group[0]['goodsSeckillStatus']==0)
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="商品未通过审核";
			echo json_encode($data);
			die();
		}
		if($goods_group[0]['seckillStatus']==0)
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="商品活动未开始";
			echo json_encode($data);
			die();
		}
		else if($goods_group[0]['seckillStatus']!=1)
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="商品活动已结束";
			echo json_encode($data);
			die();
		}
		$sum=0;
		if(isset($order_yimai))
		{
			for($j=0;$j<count($order_yimai);$j++)
			{
				$sum+=$order_yimai[$j]['goodsNums'];
			}
		}
		if($sum+$num>$goods_group[0]['seckillMaxCount'])
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="每人限购".$goods_group[0]['seckillMaxCount']."件";
			echo json_encode($data);
			die();
		}
		if($num>$goods_group[0]['seckillStock'])
		{
			$data[0]['pan']='-1';
			$data[0]['msg']="商品库存不足";
			echo json_encode($data);
			die();
		}
		$sql="select * from `".$oto."_user_address` where `userId`='".$user_id."' and `addressId`='{$address_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_address[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$dizhi='';
		for($i=0;$i<count($user_address);$i++)
		{
			$sql="select * from `".$oto."_areas` where `areaId`='".$user_address[$i]['areaId1']."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$sheng[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$sql="select * from `".$oto."_areas` where `areaId`='".$user_address[$i]['areaId2']."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$shi[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$sql="select * from `".$oto."_areas` where `areaId`='".$user_address[$i]['areaId3']."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$xian[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$dizhi=$sheng[$i]['areaName'].$shi[$i]['areaName'].$xian[$i]['areaName'].$user_address[$i]['address'];
		}
		$orderunique=floor(microtime(true)*1000);//是否同一批下单
		$createTime=date('Y-m-d H:i:s');//下单时间
		$areaId1=$user_address[0]['areaId1'];//省
		$areaId2=$user_address[0]['areaId2'];//市
		$areaId3=$user_address[0]['areaId3'];//区
		$communityId=0;//社区id
		$userAddress=$dizhi;//配送详细地址
		$userId=$user_id;//用户id
		$userName=$user_address[0]['userName'];//收货人名字
		$userPhone=$user_address[0]['userPhone'];//收货人手机号

						$orderStrNo=microtime(true);
						//$orderStrNo=time();
						$sql="INSERT INTO ".$oto."_orderids (`rnd`) VALUES ('{$orderStrNo}')";
						$result=$db->query($sql);
						//$sql="SELECT * FROM ".$oto."_orderids where rnd='{$orderStrNo}'";
						$sql="SELECT * FROM ".$oto."_orderids order by id DESC";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$orderids_info[]=$row;//将取得的所有数据赋值给person_info数组
						}
						$yongjin=0;
						$z_money=$goods_group[0]['seckillPrice']*$num;
						if($shop_info[0]['commissionCat']==2 || $shop_info[0]['commissionCat']=='2')
	            		{
	            			$yongjin=$z_money*($goods_group[0]['goodsProportion']/100);
	            		}
	            		if($shop_info[0]['deliveryFreeMoney']<=$z_money) $ps_money=0;
						else $ps_money=$shop_info[0]['deliveryMoney'];
						$needPay=$z_money;
						if($shop_info[0]['commissionCat']==1) $yongjin=$z_money*($shop_info[0]['proportion']/100);
						$orderNo=$orderids_info[0]['id']."".(fmod($orderids_info[0]['id'],7));//订单号
						$orderStatus='-2';//订单状态
						$totalMoney=$z_money;//总消费
						$deliverType=$psway;//配送方式
						$orderScore=$z_money;//获得积分
						$orderRemarks=$liuyan;//买家留言
						$requireTime=$ps_time2;//期望送达时间
						$needPay=$z_money+$ps_money;//应付金额
						$isSeckill='1';//是否秒杀商品

						
						$goodsId=$goods_id;
						$goodsNums=$num;
						$goodsPrice=$goods_group[0]['seckillPrice'];
						$goodsAttrId=$goodss_attr_id;
						$goodsAttrName=$goods_attr;
						$goodsName=$goods_group[0]['goodsName'];
						$goodsThums=$goods_group[0]['goodsImg'];
						$sql="INSERT INTO ".$oto."_orders (`orderNo`,`areaId1`,`areaId2`,`areaId3`,`shopId`,`orderStatus`,`totalMoney`,`deliverType`,`userId`,`userName`,`communityId`,`userAddress`,`userPhone`,`orderScore`,`orderRemarks`,`createTime`,`orderunique`,`needPay`,`orderType`,`payType`,`isPay`,`requireTime`,`deliverMoney`,`commission`) VALUES ('$orderNo','$areaId1','$areaId2','$areaId3','$shopId','$orderStatus','$totalMoney','$deliverType','$userId','$userName','$communityId','$userAddress','$userPhone','$orderScore','$orderRemarks','$createTime','$orderunique','$needPay','2','1','1','$requireTime','$ps_money','$yongjin')";
            			$result=$db->query($sql);//插入order表
            			//file_put_contents("tsxx.txt", "\r\n".$sql."执行：".$result."\r\n", FILE_APPEND);
            			$sql="select * from `".$oto."_orders` where `orderNo`='".$orderNo."' and `userId`='{$userId}' and `orderunique`='{$orderunique}'";
						$result=$db->query($sql);//查询刚插进去的orderId
						while($row=$result->fetch_assoc()){
							$order[0]=$row;//将取得的所有数据赋值给person_info数组
						}
						$orderId=$order[0]['orderId'];
						$data[0]['orderId']=$orderId;
						$data[0]['pan']='1';
						$data[0]['msg']="下单成功";
						$sql="INSERT INTO ".$oto."_order_reminds (`orderId`,`userId`,`shopId`,`createTime`) VALUES ('$orderId','$userId','$shopId','$createTime')";
            			$result=$db->query($sql);//插入order_reminds表
            			$sql="INSERT INTO ".$oto."_order_goods (`orderId`,`goodsId`,`goodsNums`,`goodsPrice`,`goodsAttrId`,`goodsAttrName`,`goodsName`,`goodsThums`,`goodsGroupId`) VALUES ('$orderId','$goodsId','$goodsNums','$goodsPrice','$goodsAttrId','$goodsAttrName','$goodsName','$goodsThums','0')";
            			$result=$db->query($sql);//插入order对应的order_goods表
            			$sql="update ".$oto."_goods_seckill set seckillStock=seckillStock-".$num." where goodsId='{$goods_id}'";
						$result=$db->query($sql);
	}
	else 
	{
		$data[0]['pan']='-1';
		$data[0]['msg']="商品不存在";
	}
	echo json_encode($data);
?>